<?php

App::uses('AppHelper', 'View/Helper');

/**
 * Application helper
 *
 * Add your application-wide methods in the class below, your helpers
 * will inherit them.
 *
 * @package       app.View.Helper
 * @author Duonght <hduongdmit@gmail.com>
 */
class CommonHelper extends AppHelper {

	/**
	 * Seo title
	 *
	 * @param string $string
	 * @return string
	 */
	public function rewriteTitle($string, $keyReplace = '/') {
		$string = self::ConvertToUnicode($string);

		$string = trim(preg_replace("/[^A-Za-z0-9]/i"," ",$string)); // khong dau
		$string = str_replace(" ", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace($keyReplace, "-", $string);

		return strtolower(trim($string, '-'));
	}

	/**
	 *
	 * @param unknown $str
	 * @return boolean|mixed
	 */
	public static function ConvertToUnicode($str){
		$vietnamese = array(
				"à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
				"ằ","ắ","ặ","ẳ","ẵ",
				"è","é","ẹ","ẻ","ẽ","ê","ề" ,"ế","ệ","ể","ễ",
				"ì","í","ị","ỉ","ĩ",
				"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
				,"ờ","ớ","ợ","ở","ỡ",
				"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
				"ỳ","ý","ỵ","ỷ","ỹ",
				"đ",
				"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
				,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
				"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
				"Ì","Í","Ị","Ỉ","Ĩ",
				"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
				,"Ờ","Ớ","Ợ","Ở","Ỡ",
				"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
				"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
				"Đ","ê","ù","à"
		);

		$unicode = array(
				"a","a","a","a","a","a","a","a","a","a","a"
				,"a","a","a","a","a","a",
				"e","e","e","e","e","e","e","e","e","e","e",
				"i","i","i","i","i",
				"o","o","o","o","o","o","o","o","o","o","o","o"
				,"o","o","o","o","o",
				"u","u","u","u","u","u","u","u","u","u","u",
				"y","y","y","y","y",
				"d",
				"A","A","A","A","A","A","A","A","A","A","A","A"
				,"A","A","A","A","A",
				"E","E","E","E","E","E","E","E","E","E","E",
				"I","I","I","I","I",
				"O","O","O","O","O","O","O","O","O","O","O","O"
				,"O","O","O","O","O",
				"U","U","U","U","U","U","U","U","U","U","U",
				"Y","Y","Y","Y","Y",
				"D","e","u","a"
		);

		return str_replace($vietnamese, $unicode, $str);
	}

	/**
	 * Seo title
	 *
	 * @param string $string
	 * @return string
	 */
	public function seoTitle($string) {
		$string = self::stripUnicode($string);
		$string = preg_replace("`\[.*\]`U","",$string);
		$string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i','-',$string);
		$string = htmlentities($string, ENT_COMPAT, 'utf-8');
		$string = preg_replace( "`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i","\\1", $string );
		$string = preg_replace( array("`[^a-z0-9]`i","`[-]+`") , "-", $string);
		$string = $string.'.html';
		return strtolower(trim($string, '-'));
	}

	/**
	 * Get datetime
	 *
	 * @param string $language
	 * @param number $getDay
	 * @param number $getDate
	 * @param number $getTime
	 * @param string $timeZone
	 * @param number $intTimestamp
	 * @return string
	 */
	public function getDateTime($language = "vn", $getDay = 1, $getDate = 1, $getTime = 1, $timeZone = "GMT+7", $intTimestamp = 0){
		if($intTimestamp > 0){
			$today = getdate($intTimestamp);
			$day   = $today["wday"];
			$date  = date("d/m/Y", $intTimestamp);
			$time  = date("H:i", $intTimestamp);
		} else {
			$today = getdate();
			$day   = $today["wday"];
			$date  = date("d/m/Y", time());
			$time  = date("h:i");
		}

		switch ($language){
			case "vn": $dayArray = array("Chủ nhật", "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy"); break;
			case "en": $dayArray = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); break;
			default : $dayArray = array("Chủ nhật", "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy"); break;
		}

		$strDateTime = "";
		for ($i = 0; $i <= 6; $i ++){
			if($i == $day){
				if($getDay  != 0)	$strDateTime .= $dayArray[$i] . ", ";
				if($getDate != 0)	$strDateTime .= $date . " | ";
				if($getTime != 0)	$strDateTime .= $time . " ";
				$strDateTime .= $timeZone;
				if(substr($strDateTime, -2, 2) == ", ") $strDateTime = substr($strDateTime, 0, -2);

				return $strDateTime;
			}
		}
	}

	/**
	 * Convert datetime
	 *
	 * @param string $strDate
	 * @param string $strTime
	 * @return Ambigous <>|number
	 */
	public function convertDateTime($strDate = "", $strTime = ""){
		//Break string and create array date time
		$strDate        = str_replace("/", "-", $strDate);
		$strDateArray   = explode("-", $strDate);
		$countDateArr   = count($strDateArray);
		$strTime        = str_replace("-", ":", $strTime);
		$strTimeArray   = explode(":", $strTime);
		$countTimeArr   = count($strTimeArray);

		//Get Current date time
		$today = getdate();
		$day   = $today["mday"];
		$mon   = $today["mon"];
		$year  = $today["year"];
		$hour  = $today["hours"];
		$min   = $today["minutes"];
		$sec   = $today["seconds"];

		//Get date array
		switch($countDateArr){
			case 2:
				$day = intval($strDateArray[0]);
				$mon = intval($strDateArray[1]);
				break;
			case $countDateArr >= 3:
				$day = intval($strDateArray[0]);
				$mon = intval($strDateArray[1]);
				$year = intval($strDateArray[2]);
				break;
		}

		//Get time array
		switch($countTimeArr){
			case 2:
				$hour = intval($strTimeArray[0]);
				$min = intval($strTimeArray[1]);
				break;
			case $countTimeArr >= 3:
				$hour = intval($strTimeArray[0]);
				$min = intval($strTimeArray[1]);
				$sec = intval($strTimeArray[2]);
				break;
		}

		//Return date time integer
		if(@mktime($hour, $min, $sec, $mon, $day, $year) == -1) return $today[0];
		else return mktime($hour, $min, $sec, $mon, $day, $year);
	}

	/**
	 *
	 * @param unknown $day
	 * @param unknown $month
	 * @param unknown $year
	 * @param number $hour
	 * @param number $min
	 * @param number $second
	 * @return number
	 */
	public function dateToInt($day, $month, $year, $hour = 0, $min = 0, $second = 0){
		return mktime($hour, $min, $second, $month, $day, $year);
	}


	/**
	 * Strip unicode
	 *
	 * @param string $str
	 * @return boolean|mixed
	 */
	static function stripUnicode($str){
		if(!$str) return false;
		$unicode = array(
				'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
				'd'=>'đ|Đ',
				'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
				'i'=>'í|ì|ỉ|ĩ|ị|Í|Ì|Ỉ|Ĩ|Ị',
				'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
				'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
				'y'=>'ý|ỳ|ỷ|ỹ|ỵ|Ý|Ỳ|Ỷ|Ỹ|Ỵ',
		);
		foreach($unicode as $nonUnicode=>$uni) $str = preg_replace("/($uni)/i",$nonUnicode,$str);
		return $str;
	}

	/**
	 * Cut string
	 * @param string $str
	 * @param number $len
	 * @param string $more
	 * @return string
	 */
	public static function cutstr($str, $len, $more = true) {
		if ($str=="" || $str==NULL) {
			return $str;
		}

		if (strlen($str) <= $len) {
			return $str;
		}

		$str = strip_tags($str);
		$str = trim($str);
		$str = substr($str,0,$len);
		if ($str != "") {
			if (!substr_count($str," ")) {
				if ($more) {
					$str .= " ...";
				}
				return $str;
			}

			while(strlen($str) && ($str[strlen($str)-1] != " ")) {
				$str = substr($str,0,-1);
			}

			$str = substr($str,0,-1);

			if ($more) {
				$str .= " ...";
			}
		}

		return $str;
	}

}
